package edu.scsw.rent;

import java.util.Scanner;

public class RentCalculation {

    private Scanner sc;

    public RentCalculation() {
        sc = new Scanner(System.in);
    }

    public static void main(String[] args) {
        RentCalculation rentCal = new RentCalculation();
        System.out.println("欢迎您使用房屋租赁管理系统租金计算工具!");

        boolean continueCalculation = true;
        while (continueCalculation) {
            double totalRent = rentCal.calculateTotalRent();
            System.out.println("总的租金是：" + totalRent);

            double averRent = rentCal.averageRent(totalRent);
            System.out.println("个人的租金是：" + averRent);

            System.out.print("是否继续（1：继续，2：退出）？");
            if (rentCal.sc.hasNextInt()) {
                int nextAction = rentCal.sc.nextInt();
                rentCal.sc.nextLine(); // 清除错误输入
                if (nextAction == 1) {
                    continueCalculation = true;
                }  else if (nextAction == 2) {
                    continueCalculation = false;
                } else {
                    System.out.println("您的输入有错,请重新输入！");
                    rentCal.sc.nextLine(); // 清除错误输入
                }
            } else {
                System.out.println("您的输入有错，请重新输入！");
                rentCal.sc.next(); // 清除错误输入
            }
        }
        rentCal.closeScanner(); // 关闭资源
        System.out.println("恭喜您，您已经成功退出啦！");
    }

    // 计算总的租金
    public double calculateTotalRent() {
        System.out.print("请输入月租金：");
        double monthRent = sc.nextDouble();
        sc.nextLine(); // 清除行尾的换行符

        System.out.print("请输入租房时间（以月为单位）：");
        int month = sc.nextInt();
        sc.nextLine();

        System.out.print("请输入物业费用：");
        double propertyCost = sc.nextDouble();
        sc.nextLine();

        System.out.print("请输入其它费用：");
        double otherCost = sc.nextDouble();
        sc.nextLine();

        return monthRent * month + propertyCost * month + otherCost;
    }

    // 计算个人的月租金
    public double averageRent(double totalRent) {
        System.out.print("请输入合租人数：");
        int numbers = sc.nextInt();
        sc.nextLine();
        return Math.round(totalRent / numbers); // 直接返回四舍五入的结果
    }

    // 关闭Scanner资源
    public void closeScanner() {
        if (sc != null) {
            sc.close();
        }
    }
}
